﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace Webbness.Mvc.Validators
{
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class PhoneNumberValidatorAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "'{0}' requires 888-888-8888 format.";

        public PhoneNumberValidatorAttribute() : base(_defaultErrorMessage)
        {
        }


        public override bool IsValid(object value)
        {
            var reg = new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$");
            return value == null ? false : reg.IsMatch(value.ToString());
        }
    }
}